import { useEffect } from 'react'
import { useRoutes, useLocation, useNavigate } from 'react-router-dom'
import { message } from 'antd'
import router from './router'

function ToBookingData(){
  const navigate = useNavigate()
  useEffect(() => {
    navigate('/bookingData')
    message.warning('您已登录过了！')
  }, [])
  return <></>
}
function ToLogin(){
  const navigate = useNavigate()
  useEffect(() => {
    navigate('/login')
    message.warning('您还没登录，请登录后再访问！')
  }, [])
   return <></>
}


function BeforeRouterEnter(){
  const route = useRoutes(router)
  const location = useLocation()
  let token = localStorage.getItem('token')
  
  const isRouter = ['/login']
  router[1].children?.map(item => isRouter.push(item.path))

  if(!isRouter.find(item => item === location.pathname)) return route
  
  if(location.pathname === '/login' && token) return <ToBookingData />

  if(location.pathname !== '/login' && !token) return <ToLogin /> 

  return route
}

function App() {
  
  return (
    <>
      {/* <Link to={'/home'}>home</Link> |
      <Link to={'/about'}>about</Link> |
      <Link to={'/user'}>user</Link> */}
      <BeforeRouterEnter />
      
    </>
  )
}

export default App
